package com.imdroid.carac.activities;

import java.util.Map;
import java.util.TreeMap;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;
import com.imdroid.carac.model.Team;
import com.imdroid.carac.model.TeamMember;

public class MemberListActivity extends BaseActivity{
	
	private static MemberListActivity instance;
	
	private ListView listView;
	
	public void onCreate(Bundle bdl){
		super.onCreate(bdl);
		instance = this;
		setContentView(R.layout.team_member_list);
		
		listView = (ListView) findViewById(R.id.member_list);
		listView.setAdapter(new MemberListAdapter());
	}
	
	/**
	 * 剔除队友
	 * @param view
	 */
	public void kick(View view){
		// 暂未实现
	}
	
	/**
	 * 与队友连线
	 * @param view
	 */
	public void line(View view){
		// 暂未实现
	}
	
	private class MemberListAdapter extends BaseAdapter{
		
		private TeamMember[] currentMembers;
		
		private void prepareMembers(){
			int count = getCount();
			int current = 0;
			if(count > 0){
				currentMembers = new TeamMember[count];
				for(TeamMember tm : Team.getCurrentTeam().getMembers().values()){
					if(tm.getId() == IMDroid.getUser().getUser_id())
						continue;
					currentMembers[current ++] = tm;
				}
			}
		}

		@Override
		public int getCount() {
			Team team = Team.getCurrentTeam();
			if(team == null || team.getMembers() == null)
				return 0;
			Map<Integer, TeamMember> members = team.getMembers();
			int self = 0;
			for(TeamMember tm : members.values()){
				if(tm.getId() == IMDroid.getUser().getUser_id()){
					self = 1;
					break;
				}
			}
			return members.size() - self;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(getCount() == 0){
				return null;
			}
			if(currentMembers == null){
				prepareMembers();
			}
			
			TeamMember member = currentMembers[position];
			VH vh = null;
			if(convertView == null) {
				vh = new VH();
				convertView = getLayoutInflater().inflate(R.layout.team_member_list_item, null);
				vh.name = (TextView) convertView.findViewById(R.id.name);
				vh.status = (TextView) convertView.findViewById(R.id.status);
				vh.line = (Button) convertView.findViewById(R.id.team_line);
				vh.kick = (Button) convertView.findViewById(R.id.team_kick);
				convertView.setTag(vh);
			} else {
				vh = (VH) convertView.getTag();
			}
			vh.line.setTag(member);
			vh.kick.setTag(member);
			vh.name.setText(member.getDisplayName());
			BDLocation loc = IMDroid.getCurrentLocation();
			double dist = Team.getDistance(loc.getLatitude(), loc.getLongitude(), member.getLat(), member.getLon());
			String unit = null;
			if(dist > 1000){
				dist = Math.round(dist / 1000);
				unit = "公里";
			} else {
				unit = "米";
			}
			vh.status.setText( dist + unit + " -- " + (member.getSpeed() * 3.6) + "公里/小时");
			if(IMDroid.getUser().getUser_id() == Team.getCurrentTeam().getTeamId()) {
				vh.kick.setVisibility(View.VISIBLE);
			} else {
				vh.kick.setVisibility(View.GONE);
			}
			
			return convertView;
		}
		
		public void refresh(){
			prepareMembers();
			this.notifyDataSetChanged();
		}
		
		private class VH {
			private TextView name;
			private TextView status;
			private Button line;
			private Button kick;
		}
		
	}
	
	public static class MemberMap<K, V> extends TreeMap<K, V>{

		/**
		 * 
		 */
		private static final long serialVersionUID = -5074583749617379088L;

		/* (non-Javadoc)
		 * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
		 */
		@Override
		public V put(K key, V value) {
			V v = super.put(key, value);
			if(instance != null && instance.listView != null && instance.listView.getAdapter() != null){
				((MemberListAdapter) (instance.listView.getAdapter())).refresh();
			}
			return v;
		}

		/* (non-Javadoc)
		 * @see java.util.HashMap#remove(java.lang.Object)
		 */
		@Override
		public V remove(Object key) {
			V v = super.remove(key);
			if(instance != null && instance.listView != null && instance.listView.getAdapter() != null){
				((MemberListAdapter) (instance.listView.getAdapter())).refresh();
			}
			return v;
		}
	}

}
